package com.yc.shopping.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FillLayout;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.shopping.dao.GoodDao;
import com.yc.shopping.dao.MenberDao;
import com.yc.shopping.util.ShoppingUtil;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Button;

public class SaleMainUI {

	protected Shell shell;
	private Text text_oid;
	private Text text_aname;
	private Text text_mid;
	private Table table;
	private Text text_gname;
	private Text text_num;
	private Text text_saleprice;
	private Text text_total;
	private Text text_gid;
	
	//商品的Dao类
	GoodDao goodsDao = new GoodDao();
	MenberDao menberDao = new MenberDao();
	private List<Map<String,Object>> carts = null;//购物车 默认为空

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			SaleMainUI window = new SaleMainUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(1463, 1104);
		shell.setText("源辰收银系统");
		shell.setMaximized(true);
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		SashForm sashForm = new SashForm(shell, SWT.VERTICAL);
		
		Composite composite_2 = new Composite(sashForm, SWT.NONE);
		composite_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		
		Label label = new Label(composite_2, SWT.SHADOW_NONE | SWT.CENTER);
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 9, SWT.NORMAL));
		label.setBounds(10, 10, 129, 24);
		label.setText("流水号(订单号):");
		
		text_oid = new Text(composite_2, SWT.BORDER | SWT.READ_ONLY);
		text_oid.setText("YC202211062019");
		text_oid.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		text_oid.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_oid.setBounds(151, 7, 164, 30);
		
		Label label_1 = new Label(composite_2, SWT.SHADOW_NONE | SWT.CENTER);
		label_1.setText("操作员:");
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 9, SWT.NORMAL));
		label_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label_1.setBounds(356, 10, 129, 24);
		
		text_aname = new Text(composite_2, SWT.BORDER | SWT.READ_ONLY);
		text_aname.setText("lisi");
		text_aname.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_aname.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		text_aname.setBounds(494, 7, 164, 30);
		
		Label label_2 = new Label(composite_2, SWT.SHADOW_NONE | SWT.CENTER);
		label_2.setText("会员号");
		label_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 9, SWT.NORMAL));
		label_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label_2.setBounds(719, 10, 129, 24);
		
		text_mid = new Text(composite_2, SWT.BORDER | SWT.READ_ONLY);
		text_mid.setText("无");
		text_mid.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_mid.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		text_mid.setBounds(866, 7, 164, 30);
		//中间面板 左右2部分 此面板也需要设置为填充式布局
		Composite composite_1 = new Composite(sashForm, SWT.NONE);
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		SashForm sashForm_1 = new SashForm(composite_1, SWT.NONE);
		
		Composite composite_3 = new Composite(sashForm_1, SWT.NONE);
		composite_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		
		table = new Table(composite_3, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 10, 1287, 418);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("商品编号");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(149);
		tableColumn_1.setText("商品条形码");
		
		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(100);
		tableColumn_2.setText("商品名称");
		
		TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
		tableColumn_3.setWidth(100);
		tableColumn_3.setText("销售价");
		
		TableColumn tableColumn_5 = new TableColumn(table, SWT.NONE);
		tableColumn_5.setWidth(100);
		tableColumn_5.setText("数量");
		
		TableColumn tableColumn_4 = new TableColumn(table, SWT.NONE);
		tableColumn_4.setWidth(100);
		tableColumn_4.setText("金额");
		
		Label label_3 = new Label(composite_3, SWT.SHADOW_NONE | SWT.CENTER);
		label_3.setText("商品名称:");
		label_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_3.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 9, SWT.NORMAL));
		label_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label_3.setBounds(10, 498, 129, 27);
		
		text_gname = new Text(composite_3, SWT.BORDER | SWT.READ_ONLY);
		text_gname.setText("猪肉脯");
		text_gname.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_gname.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		text_gname.setBounds(164, 495, 164, 30);
		
		Label label_3_1 = new Label(composite_3, SWT.SHADOW_NONE | SWT.CENTER);
		label_3_1.setText("购买数量:");
		label_3_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_3_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 9, SWT.NORMAL));
		label_3_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label_3_1.setBounds(10, 586, 129, 27);
		
		text_num = new Text(composite_3, SWT.BORDER | SWT.READ_ONLY);
		text_num.setText("10");
		text_num.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_num.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		text_num.setBounds(164, 583, 164, 30);
		
		Label label_3_2 = new Label(composite_3, SWT.SHADOW_NONE | SWT.CENTER);
		label_3_2.setText("商品单价");
		label_3_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_3_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 9, SWT.NORMAL));
		label_3_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label_3_2.setBounds(382, 498, 129, 27);
		
		text_saleprice = new Text(composite_3, SWT.BORDER | SWT.READ_ONLY);
		text_saleprice.setText("10.0");
		text_saleprice.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_saleprice.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		text_saleprice.setBounds(536, 495, 164, 30);
		
		Label label_3_3 = new Label(composite_3, SWT.SHADOW_NONE | SWT.CENTER);
		label_3_3.setText("商品金额");
		label_3_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_3_3.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 9, SWT.NORMAL));
		label_3_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label_3_3.setBounds(382, 586, 129, 27);
		
		text_total = new Text(composite_3, SWT.BORDER | SWT.READ_ONLY);
		text_total.setText("100");
		text_total.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text_total.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		text_total.setBounds(536, 583, 164, 30);
		
		Composite composite_4 = new Composite(sashForm_1, SWT.NONE);
		composite_4.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		sashForm_1.setWeights(new int[] {10, 1});
		
		Composite composite = new Composite(sashForm, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		
		Label label_3_4 = new Label(composite, SWT.SHADOW_NONE | SWT.CENTER);
		label_3_4.setText("商品:");
		label_3_4.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label_3_4.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 9, SWT.NORMAL));
		label_3_4.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label_3_4.setBounds(10, 10, 129, 27);
		
		text_gid = new Text(composite, SWT.BORDER);
		text_gid.setBounds(129, 7, 221, 30);
		
		Button btn_pay = new Button(composite, SWT.NONE);
		btn_pay.setBounds(418, 10, 114, 34);
		btn_pay.setText("结账F5");
		
		Button btnf_update = new Button(composite, SWT.NONE);
		btnf_update.setBounds(590, 10, 114, 34);
		btnf_update.setText("修改数量F6");
		
		Button btn_exit = new Button(composite, SWT.NONE);
		btn_exit.setBounds(768, 10, 114, 34);
		btn_exit.setText("退货");
		
		Button btn_count = new Button(composite, SWT.NONE);
		btn_count.setBounds(954, 10, 114, 34);
		btn_count.setText("报表统计");
		
		Button btn_set = new Button(composite, SWT.NONE);
		btn_set.setBounds(1168, 10, 114, 34);
		btn_set.setText("系统设置");
		sashForm.setWeights(new int[] {36, 370, 40});
		
		
		//生成流水号
		text_oid.setText(ShoppingUtil.genOrderId());
		
		Button btn_menber = new Button(composite_2, SWT.NONE);
		btn_menber.setBounds(1190, 10, 114, 34);
		btn_menber.setText("添加会员");

		
		
		//添加全局快捷键
		Display.getCurrent().addFilter(SWT.KeyDown, new Listener() {

			@Override
			public void handleEvent(Event e) {
				if(e.keyCode == SWT.F5) {
					payOrder();//结账
				}else if(e.keyCode == SWT.F6) {
					updateNum();
				}else if(e.keyCode == 13) {
					addCart();
				}
			}
			
		});
		
		
		
		
		
		
		
		
		//加入登录判断 如果未登录则跳转到SaleLoginUI
	}
	
	/*
	 * 添加购物车
	 */
	public void addCart() {
		//从界面上获取文本框中输入的内容
		String code = text_gid.getText().trim();
		//1.根据文本框输入编号查询商品信息
		try {
			Map<String, Object> map = goodsDao.findByidOrcode(code);
			if(map == null || map.isEmpty()) {
				ShoppingUtil.showMessage(shell, "消息提示", "查无此商品");
				text_gid.setText("");//清空文本框
				return;
			}
			//库存判断
			int balance = Integer.parseInt(ShoppingUtil.objToString(map.get("BALANCE")));
			if(balance <= 0) {
				ShoppingUtil.showMessage(shell, "消息提示", "库存不足");
				text_gid.setText("");//清空文本框
				return;
			}
			
			//判断购物车是否为空
			if(carts == null) {
				carts = new ArrayList<Map<String,Object>>();//创建购物车
			}
			Map<String,Object> m = new HashMap<String,Object>();
			m.put("GID", map.get("GID"));
			m.put("GCODE", map.get("GCODE"));
			m.put("GNAME", map.get("GNAME"));
			m.put("SALEPRICE", map.get("SALEPRICE"));
			m.put("NUM", 1);//默认数量为1
			m.put("TOTAL", map.get("SALEPRICE"));//当前商品总金额
			carts.add(m);
			text_gid.setText("");
			
			//设置文本框
			text_gname.setText(ShoppingUtil.objToString(m.get("GNAME")));
			text_saleprice.setText(ShoppingUtil.objToString(m.get("SALEPRICE")));
			text_num.setText(ShoppingUtil.objToString(m.get("NUM")));
			text_total.setText(ShoppingUtil.objToString(m.get("TOTAL")));

			//显示购物车中所有数据
			showCart();
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	//显示购物车中的信息
	private void showCart() {
		table.removeAll();//清空表格
		if(carts == null) {
			return;
		}
		TableItem item = null;
		for(Map<String,Object> m:carts) {
			item = new TableItem(table,SWT.NONE);
			item.setText(new String[] {
					ShoppingUtil.objToString(m.get("GID")),
					ShoppingUtil.objToString(m.get("GCODE")),
					ShoppingUtil.objToString(m.get("GNAME")),
					ShoppingUtil.objToString(m.get("SALEPRICE")),
					ShoppingUtil.objToString(m.get("NUM")),
					ShoppingUtil.objToString(m.get("TOTAL")),

			});
		}
	}
	
	/*
	 * 修改购买商品数量
	 */
	private void updateNum() {
		//从界面上获取文本框中输入的内容
		String code = text_gid.getText().trim();
		//1.根据文本框输入编号查询商品信息
		try {
		Map<String, Object> map = goodsDao.findByidOrcode(code);
			if(map == null || map.isEmpty()) {
			ShoppingUtil.showMessage(shell, "消息提示", "查无此商品");
			text_gid.setText("");//清空文本框
			return;
			}
			
			//弹框输入数量
			InputDialog id = new InputDialog(shell,"商品数量","请输入购买数量","1",null);
			id.open();
			String num = id.getValue();//获取输入的值
			//库存判断
			int balance = Integer.parseInt(ShoppingUtil.objToString(map.get("BALANCE")));
			if(balance < Integer.parseInt(num)) {
				ShoppingUtil.showMessage(shell, "消息提示", "库存不足");
				text_gid.setText("");//清空文本框
				return ;
			}
			float total = Integer.parseInt(num) * Float.parseFloat(ShoppingUtil.objToString(map.get("SALEPRICE"))  );
			
			
			//判断购物车是否为空
			if(carts == null) {
				carts = new ArrayList<Map<String,Object>>();//创建购物车
				}
			Map<String,Object> m = new HashMap<String,Object>();
			m.put("GID", map.get("GID"));
			m.put("GCODE", map.get("GCODE"));
			m.put("GNAME", map.get("GNAME"));
			m.put("SALEPRICE", map.get("SALEPRICE"));
			m.put("NUM", num);
			m.put("TOTAL", map.get("SALEPRICE"));//当前商品总金额
			carts.add(m);
			text_gid.setText("");
					
			//设置文本框
			text_gname.setText(ShoppingUtil.objToString(m.get("GNAME")));
			text_saleprice.setText(ShoppingUtil.objToString(m.get("SALEPRICE")));
			text_num.setText(ShoppingUtil.objToString(m.get("NUM")));
			text_total.setText(ShoppingUtil.objToString(m.get("TOTAL")));

			//显示购物车中所有数据
			showCart();
					
					
					
					
			} catch (SQLException e) {
				e.printStackTrace();
				}
	}
	
	/*
	 * 结账
	 */
	private void payOrder() {
		
		//输入会员
		InputDialog id = new InputDialog(shell,"会员卡","请输入会员卡","无",null);
		id.open();
		String num = id.getValue();//获取输入的值
		text_mid.setText(num);//设置会员卡
		//计算
		if(carts == null) {
			ShoppingUtil.showMessage(shell, "消息提示", "购物车为空");
			return;
		}
		
		int count = 0;//商品总数量
		for(Map<String,Object> m:carts) {
			
			int buy_num = Integer.parseInt(ShoppingUtil.objToString(m.get("NUM")));
			float total = Float.parseFloat(ShoppingUtil.objToString(m.get("TOTAL")));
			count += buy_num;
			ShoppingUtil.totalPrice +=total;
		}
		
		//将数据存储到order表盒OrderItem表中
		//1.订单表中插入一条数据
		//2.订单详情表 中插入多条数据	carts中有几条数据	就要插几条数据
		
		
		
		
		
		
		
		
		
		//弹框
		Dialog_PayOrder pay = new Dialog_PayOrder(shell,SWT.CLOSE|SWT.APPLICATION_MODAL);
		pay.open();
		
		//TODO
		int currscore = (int)ShoppingUtil.totalPrice*10;
		String mid = text_mid.getText();
		if(!"无".equals(mid)) {
			try {
				menberDao.updateScore(mid, currscore);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		//支付成功后，清空购物车 	会员卡编号	生成一个新流水账号	供下一个订单使用
		carts = null;
		table.removeAll();
		ShoppingUtil.totalPrice = 0;
		text_mid.setText("无");
		text_oid.setText(ShoppingUtil.genOrderId());
		//清空只读文本框
		text_gname.setText("");
		text_num.setText("");
		text_saleprice.setText("");
		text_total.setText("");
		
	}

}
